﻿#include "modifystation.h"
#include "../include/settings.h"

#include <QDebug>

/*==================================================================    
 *    功能：   用于构造修改工作站界面， no为工作站编号，type指定
 *    输入参数：
 *    返回值： 
/*==================================================================*/
ModifyStation::ModifyStation(int no, QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);
#if 1
	setWindowFlags(Qt::Window | Qt::FramelessWindowHint
		| Qt::WindowSystemMenuHint | Qt::WindowMinimizeButtonHint
		| Qt::WindowMaximizeButtonHint);

	this->setAutoFillBackground(true);
	QPalette palette;
	palette.setColor(QPalette::Background, QColor(30,144,255));
	this->setPalette(palette);

	QPainterPath path;
	QRectF rect = QRectF(this->rect());
	path.addRoundRect(rect,5,5);
	QPolygon polygon= path.toFillPolygon().toPolygon();
	QRegion region(polygon);
	setMask(region);
#endif
	type = STATION;
	initModifyStation(no);
}

ModifyStation::ModifyStation(QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);
#if 1
	setWindowFlags(Qt::Window | Qt::FramelessWindowHint
		| Qt::WindowSystemMenuHint | Qt::WindowMinimizeButtonHint
		| Qt::WindowMaximizeButtonHint);

	this->setAutoFillBackground(true);
	QPalette palette;
	palette.setColor(QPalette::Background, QColor(30,144,255));
	this->setPalette(palette);

	QPainterPath path;
	QRectF rect = QRectF(this->rect());
	path.addRoundRect(rect,5,5);
	QPolygon polygon= path.toFillPolygon().toPolygon();
	QRegion region(polygon);
	setMask(region);
#endif
	type = STATION_ADD;
	initModifyStation(-1);
}

/*==================================================================    
 *    功能：   初始化
 *    输入参数：no为工作站编号，type为工作站类型
 *    返回值： 
/*==================================================================*/
void ModifyStation::initModifyStation(int no)
{
	setModal(true);
#if 0
	setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowSystemMenuHint 
		| Qt::WindowMinimizeButtonHint
		| Qt::WindowMaximizeButtonHint);

	//setAttribute(Qt::WA_DeleteOnClose);

	this->setAutoFillBackground(true);
	QPalette palette;
	palette.setColor(QPalette::Background, QColor(35,202,255));
	//this->setPalette(palette);

	QPainterPath path;
	QRectF rect = QRectF(this->rect());
	path.addRoundRect(rect,10,10);
	QPolygon polygon= path.toFillPolygon().toPolygon();
	QRegion region(polygon);
	setMask(region);
#endif
	//当点击确定时，执行的槽函数为ok
	connect(ui.okButton, SIGNAL(clicked()), this, SLOT(ok()));
    connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(close()));

	//记录当前焦点所在采集站的编号
	index = no;

	//读取已有的配置或增加新配置
	readIni(no);
}

/*==================================================================    
 *    功能：   
 *    输入参数：如果type为STATION，则是读取已有的配置。如为STATION_ADD则增加新配置页
 *    返回值： 
/*==================================================================*/
void ModifyStation::readIni(int no)
{
	//获取当前工作站数目
	int sNum = Settings::readStationNum();

	//添加工作站的页面
	if(type == STATION_ADD) {
		++sNum;
		ui.lineEdit_stationName->setText(Settings::readName(sNum));
		return;
	}
	//临时设置当前待添加station的名字
	ui.lineEdit_stationName->setText(Settings::readName(no));
	//当修改或者查看其他station
	ui.lineEdit_note->setText(Settings::readNote(no));

	//更新checkbox传感器
	checkMask(no);
}

/*==================================================================    
 *    功能：   检测传感器标志掩码
 *    输入参数：no为采集站编号
 *    返回值： 
/*==================================================================*/
void ModifyStation::checkMask(int no)
{
	unsigned int maskg = Settings::readMask(no,DATA_G);
	if(maskg & 0x0001)
		ui.checkBox_g1->setChecked(true);
	else
		ui.checkBox_g1->setChecked(false);
	if(maskg & 0x0002)
		ui.checkBox_g2->setChecked(true);
	else
		ui.checkBox_g2->setChecked(false);
	if(maskg & 0x0004)
		ui.checkBox_g3->setChecked(true);
	else
		ui.checkBox_g3->setChecked(false);
	if(maskg & 0x0008)
		ui.checkBox_g4->setChecked(true);
	else
		ui.checkBox_g4->setChecked(false);
	if(maskg & 0x0010)
		ui.checkBox_g5->setChecked(true);
	else
		ui.checkBox_g5->setChecked(false);

	if(maskg & 0x0020)
		ui.checkBox_g6->setChecked(true);
	else
		ui.checkBox_g6->setChecked(false);



	unsigned int masky = Settings::readMask(no,DATA_Y);
	if(masky & 0x0001)
		ui.checkBox_y1->setChecked(true);
	else
		ui.checkBox_y1->setChecked(false);
	if(masky & 0x0002)
		ui.checkBox_y2->setChecked(true);
	else
		ui.checkBox_y2->setChecked(false);
	if(masky & 0x0004)
		ui.checkBox_y3->setChecked(true);
	else
		ui.checkBox_y3->setChecked(false);
	if(masky & 0x0008)
		ui.checkBox_y4->setChecked(true);
	else
		ui.checkBox_y4->setChecked(false);
	if(masky & 0x0010)
		ui.checkBox_y5->setChecked(true);
	else
		ui.checkBox_y5->setChecked(false);

	if(masky & 0x0020)
		ui.checkBox_y6->setChecked(true);
	else
		ui.checkBox_y6->setChecked(false);

	if(masky & 0x0040)
		ui.checkBox_y7->setChecked(true);
	else
		ui.checkBox_y7->setChecked(false);
	if(masky & 0x0080)
		ui.checkBox_y8->setChecked(true);
	else
		ui.checkBox_y8->setChecked(false);
	if(masky & 0x0100)
		ui.checkBox_y9->setChecked(true);
	else
		ui.checkBox_y9->setChecked(false);
	if(masky & 0x0200)
		ui.checkBox_y10->setChecked(true);
	else
		ui.checkBox_y10->setChecked(false);
	if(masky & 0x0400)
		ui.checkBox_y11->setChecked(true);
	else
		ui.checkBox_y11->setChecked(false);

	if(masky & 0x0800)
		ui.checkBox_y12->setChecked(true);
	else
		ui.checkBox_y12->setChecked(false);
	//qDebug()<<"【READ】Masky:"<<maskg<<"  【READ】Masky:"<<masky<<endl;
}

/*==================================================================    
 *    功能：   增加新配置或者更新已有配置
 *    输入参数：
 *    返回值： 
/*==================================================================*/
void ModifyStation::ok()
{
	//ui.checkBox_g1->isChecked(false);
	unsigned int maskg = 0x0000;
	if(ui.checkBox_g1->isChecked()) {
		maskg |= 0x0001;
	}
	if(ui.checkBox_g2->isChecked()) {
		maskg |= 0x0002;
	}
	if(ui.checkBox_g3->isChecked()) {
		maskg |= 0x0004;
	}
	if(ui.checkBox_g4->isChecked()) {
		maskg |= 0x0008;
	}
	if(ui.checkBox_g5->isChecked()) {
		maskg |= 0x0010;
	}
	if(ui.checkBox_g6->isChecked()) {
		maskg |= 0x0020;
	}
	//qDebug()<<"Maskg:"<<maskg<<endl;
	unsigned int masky = 0x00;
	if(ui.checkBox_y1->isChecked()) {
		masky |= 0x0001;
	}
	if(ui.checkBox_y2->isChecked()) {
		masky |= 0x0002;
	}
	if(ui.checkBox_y3->isChecked()) {
		masky |= 0x0004;
	}
	if(ui.checkBox_y4->isChecked()) {
		masky |= 0x0008;
	}
	if(ui.checkBox_y5->isChecked()) {
		masky |= 0x0010;
	}
	if(ui.checkBox_y6->isChecked()) {
		masky |= 0x0020;
	}
	if(ui.checkBox_y7->isChecked()) {
		masky |= 0x0040;
	}
	if(ui.checkBox_y8->isChecked()) {
		masky |= 0x0080;
	}
	if(ui.checkBox_y9->isChecked()) {
		masky |= 0x0100;
	}
	if(ui.checkBox_y10->isChecked()) {
		masky |= 0x0200;
	}
	if(ui.checkBox_y11->isChecked()) {
		masky |= 0x0400;
	}
	if(ui.checkBox_y12->isChecked()) {
		masky |= 0x0800;
	}
		//
	//
	qDebug()<<"【WRITE】Masky:"<<maskg<<"  【WRITE】Masky:"<<masky<<endl;

	QString note = ui.lineEdit_note->text();
	if(type == STATION){
		//更新已有配置
		Settings::updateMaskAndNote(index, maskg,masky, note);
		emit updateStationSignal(index);
	}else {
		//增加新的采集站配置
		Settings::addStation(maskg,masky, note);
		emit addNewStationSignal();
	}
	this->close();
}

void ModifyStation::closeEvent(QCloseEvent *)
{
	delete this;
}

ModifyStation::~ModifyStation()
{
}
